home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / pcl / sptmbr11.lha / clx / translate.lisp < prev   
Lisp/Scheme  |  1992-03-24  |  24KB  |  564 lines

  1. ;;; -*- Mode:Lisp; Package:XLIB; Syntax:COMMON-LISP; Base:10; Lowercase:YES -*-
  2.  
  3. ;;;
  4. ;;;             TEXAS INSTRUMENTS INCORPORATED
  5. ;;;                  P.O. BOX 2909
  6. ;;;                   AUSTIN, TEXAS 78769
  7. ;;;
  8. ;;; Copyright (C) 1987 Texas Instruments Incorporated.
  9. ;;;
  10. ;;; Permission is granted to any individual or institution to use, copy, modify,
  11. ;;; and distribute this software, provided that this complete copyright and
  12. ;;; permission notice is maintained, intact, in all copies and supporting
  13. ;;; documentation.
  14. ;;;
  15. ;;; Texas Instruments Incorporated provides this software "as is" without
  16. ;;; express or implied warranty.
  17. ;;;
  18.  
  19. (in-package :xlib)
  20.  
  21. (defvar *keysym-sets* nil) ;; Alist of (name first-keysym last-keysym)
  22.  
  23. (defun define-keysym-set (set first-keysym last-keysym)
  24.   ;; Define all keysyms from first-keysym up to and including
  25.   ;; last-keysym to be in SET (returned from the keysym-set function).
  26.   ;; Signals an error if the keysym range overlaps an existing set.
  27.  (declare (type keyword set)
  28.       (type keysym first-keysym last-keysym))
  29.   (when (> first-keysym last-keysym)
  30.     (rotatef first-keysym last-keysym))
  31.   (setq *keysym-sets* (delete set *keysym-sets* :key #'car))
  32.   (dolist (set *keysym-sets*)
  33.     (let ((first (second set))
  34.       (last (third set)))
  35.       (when (or (<= first first-keysym last)
  36.         (<= first last-keysym last))
  37.     (error "Keysym range overlaps existing set ~s" set))))
  38.   (push (list set first-keysym last-keysym) *keysym-sets*)
  39.   set)
  40.  
  41. (defun keysym-set (keysym)
  42.   ;; Return the character code set name of keysym
  43.   (declare (type keysym keysym)
  44.        (values keyword))
  45.   (dolist (set *keysym-sets*)
  46.     (let ((first (second set))
  47.       (last (third set)))
  48.       (when (<= first keysym last)
  49.     (return (first set))))))
  50.  
  51. (eval-when (compile eval load) ;; Required for Vaxlisp ...
  52. (defmacro keysym (keysym &rest bytes)
  53.   ;; Build a keysym.
  54.   ;; If KEYSYM is an integer, it is used as the most significant bits of
  55.   ;; the keysym, and BYTES are used to specify low order bytes. The last
  56.   ;; parameter is always byte4 of the keysym.  If KEYSYM is not an
  57.   ;; integer, the keysym associated with KEYSYM is returned.
  58.   ;;
  59.   ;; This is a macro and not a function macro to promote compile-time
  60.   ;; lookup. All arguments are evaluated.
  61.   (declare (type t keysym)
  62.        (type list bytes)
  63.        (values keysym))
  64.   (typecase keysym
  65.     ((integer 0 *)
  66.      (dolist (b bytes keysym) (setq keysym (+ (ash keysym 8) b))))
  67.     (otherwise
  68.      (or (car (character->keysyms keysym))
  69.      (error "~s Isn't the name of a keysym" keysym)))))
  70. )
  71.  
  72. (defvar *keysym->character-map*
  73.     (make-hash-table :test (keysym->character-map-test) :size 400))
  74.  
  75. ;; Keysym-mappings are a list of the form (object translate lowercase modifiers mask)
  76. ;; With the following accessor macros. Everything after OBJECT is optional.
  77.  
  78. (defmacro keysym-mapping-object (keysym-mapping)
  79.   ;; Parameter to translate
  80.   `(first ,keysym-mapping))
  81.  
  82. (defmacro keysym-mapping-translate (keysym-mapping)
  83.   ;; Function to be called with parameters (display state OBJECT)
  84.   ;; when translating KEYSYM and modifiers and mask are satisfied.
  85.   `(second ,keysym-mapping))
  86.  
  87. (defmacro keysym-mapping-lowercase (keysym-mapping)
  88.   ;; LOWERCASE is used for uppercase alphabetic keysyms.  The value
  89.   ;; is the associated lowercase keysym.
  90.   `(third ,keysym-mapping))
  91.  
  92. (defmacro keysym-mapping-modifiers (keysym-mapping)
  93.   ;; MODIFIERS is either a modifier-mask or list containing intermixed
  94.   ;; keysyms and state-mask-keys specifying when to use this
  95.   ;; keysym-translation.
  96.   `(fourth ,keysym-mapping))
  97.  
  98. (defmacro keysym-mapping-mask (keysym-mapping)
  99.   ;; MASK is either a modifier-mask or list containing intermixed
  100.   ;; keysyms and state-mask-keys specifying which modifiers to look at
  101.   ;; (i.e.  modifiers not specified are don't-cares)
  102.   `(fifth ,keysym-mapping))
  103.  
  104. (defvar *default-keysym-translate-mask*
  105.     (the (or (member :modifiers) mask16 list)    ; (list (or keysym state-mask-key))
  106.          (logand #xff (lognot (make-state-mask :lock))))
  107.   "Default keysym state mask to use during keysym-translation.")
  108.  
  109. (defun define-keysym (object keysym &key lowercase translate modifiers mask display)                  
  110.   ;; Define the translation from keysym/modifiers to a (usually
  111.   ;; character) object.  ANy previous keysym definition with
  112.   ;; KEYSYM and MODIFIERS is deleted before adding the new definition.
  113.   ;;
  114.   ;; MODIFIERS is either a modifier-mask or list containing intermixed
  115.   ;; keysyms and state-mask-keys specifying when to use this
  116.   ;; keysym-translation.  The default is NIL.
  117.   ;;
  118.   ;; MASK is either a modifier-mask or list containing intermixed
  119.   ;; keysyms and state-mask-keys specifying which modifiers to look at
  120.   ;; (i.e.  modifiers not specified are don't-cares).
  121.   ;; If mask is :MODIFIERS then the mask is the same as the modifiers
  122.   ;; (i.e.  modifiers not specified by modifiers are don't cares)
  123.   ;; The default mask is *default-keysym-translate-mask*
  124.   ;;
  125.   ;; If DISPLAY is specified, the translation will be local to DISPLAY,
  126.   ;; otherwise it will be the default translation for all displays.
  127.   ;;
  128.   ;; LOWERCASE is used for uppercase alphabetic keysyms.  The value
  129.   ;; is the associated lowercase keysym.  This information is used
  130.   ;; by the keysym-both-case-p predicate (for caps-lock computations)
  131.   ;; and by the keysym-downcase function.
  132.   ;;
  133.   ;; TRANSLATE will be called with parameters (display state OBJECT)
  134.   ;; when translating KEYSYM and modifiers and mask are satisfied.
  135.   ;; [e.g (zerop (logxor (logand state (or mask *default-keysym-translate-mask*))
  136.   ;;                     (or modifiers 0)))
  137.   ;;      when mask and modifiers aren't lists of keysyms]
  138.   ;; The default is #'default-keysym-translate
  139.   ;;
  140.   (declare (type (or base-char t) object)
  141.        (type keysym keysym)
  142.        (type (or null mask16 list) ;; (list (or keysym state-mask-key))
  143.              modifiers)
  144.        (type (or null (member :modifiers) mask16 list) ;; (list (or keysym state-mask-key))
  145.              mask)
  146.        (type (or null display) display)
  147.            (type (or null keysym) lowercase)
  148.        (type (or null (function (display card16 t) t)) translate))
  149.   (flet ((merge-keysym-mappings (new old)
  150.        ;; Merge new keysym-mapping with list of old mappings.
  151.        ;; Ensure that the mapping with no modifiers or mask comes first.
  152.        (let* ((key (keysym-mapping-modifiers new))
  153.           (merge (delete key old :key #'cadddr :test #'equal)))
  154.          (if key
  155.          (nconc merge (list new))
  156.            (cons new merge))))
  157.      (mask-check (mask)
  158.        (unless (or (numberp mask)
  159.                (dolist (element mask t)
  160.              (unless (or (find element *state-mask-vector*)
  161.                      (gethash element *keysym->character-map*))
  162.                (return nil))))
  163.          (x-type-error mask '(or mask16 (list (or modifier-key modifier-keysym)))))))
  164.     (let ((entry
  165.         ;; Create with a single LIST call, to ensure cdr-coding
  166.         (cond
  167.           (mask
  168.            (unless (eq mask :modifiers)
  169.          (mask-check mask))
  170.            (when (or (null modifiers) (and (numberp modifiers) (zerop modifiers)))
  171.          (error "Mask with no modifiers"))
  172.            (list object translate lowercase modifiers mask))
  173.           (modifiers (mask-check modifiers)
  174.              (list object translate lowercase modifiers))
  175.           (lowercase    (list object translate lowercase))
  176.           (translate    (list object translate))
  177.           (t    (list object)))))
  178.       (if display
  179.       (let ((previous (assoc keysym (display-keysym-translation display))))
  180.         (if previous
  181.         (setf (cdr previous) (merge-keysym-mappings entry (cdr previous)))
  182.           (push (list keysym entry) (display-keysym-translation display))))
  183.     (setf (gethash keysym *keysym->character-map*)
  184.           (merge-keysym-mappings entry (gethash keysym *keysym->character-map*)))))
  185.     object))
  186.  
  187. (defun undefine-keysym (object keysym &key display modifiers &allow-other-keys)                  
  188.   ;; Undefine the keysym-translation translating KEYSYM to OBJECT with MODIFIERS.
  189.   ;; If DISPLAY is non-nil, undefine the translation for DISPLAY if it exists.
  190.   (declare (type (or base-char t) object)
  191.        (type keysym keysym)
  192.        (type (or null mask16 list) ;; (list (or keysym state-mask-key))
  193.              modifiers)
  194.        (type (or null display) display))
  195.   (flet ((match (key entry)
  196.        (let ((object (car key))
  197.          (modifiers (cdr key)))
  198.          (or (eql object (keysym-mapping-object entry))
  199.          (equal modifiers (keysym-mapping-modifiers entry))))))
  200.     (let* (entry
  201.        (previous (if display
  202.              (cdr (setq entry (assoc keysym (display-keysym-translation display))))
  203.                (gethash keysym *keysym->character-map*)))
  204.        (key (cons object modifiers)))
  205.       (when (and previous (find key previous :test #'match))
  206.     (setq previous (delete key previous :test #'match))
  207.     (if display
  208.         (setf (cdr entry) previous)
  209.       (setf (gethash keysym *keysym->character-map*) previous))))))
  210.  
  211. (defun keysym-downcase (keysym)
  212.   ;; If keysym has a lower-case equivalent, return it, otherwise return keysym.
  213.   (declare (type keysym keysym))
  214.   (declare (values keysym))
  215.   (let ((translations (gethash keysym *keysym->character-map*)))
  216.     (or (and translations (keysym-mapping-lowercase (first translations))) keysym)))
  217.  
  218. (defun keysym-uppercase-alphabetic-p (keysym)
  219.   ;; Returns T if keysym is uppercase-alphabetic.
  220.   ;; I.E. If it has a lowercase equivalent.
  221.   (declare (type keysym keysym))
  222.   (declare (values (or null keysym)))
  223.   (let ((translations (gethash keysym *keysym->character-map*)))
  224.     (and translations
  225.      (keysym-mapping-lowercase (first translations)))))
  226.  
  227. (defun character->keysyms (character &optional display)
  228.   ;; Given a character, return a list of all matching keysyms.
  229.   ;; If DISPLAY is given, translations specific to DISPLAY are used,
  230.   ;; otherwise only global translations are used.
  231.   ;; Implementation dependent function.
  232.   ;; May be slow [i.e. do a linear search over all known keysyms]
  233.   (declare (type t character)
  234.        (type (or null display) display)
  235.        (values (list keysym)))
  236.   (let ((result nil))
  237.     (when display
  238.       (dolist (mapping (display-keysym-translation display))
  239.     (when (eql character (second mapping))
  240.       (push (first mapping) result))))
  241.     (maphash #'(lambda (keysym mappings)
  242.          (dolist (mapping mappings)
  243.            (when (eql (keysym-mapping-object mapping) character)
  244.              (pushnew keysym result))))
  245.          *keysym->character-map*)
  246.     result))
  247.  
  248. (eval-when (compile eval load) ;; Required for Symbolics...
  249. (defconstant character-set-switch-keysym (keysym 255 126))
  250. (defconstant left-shift-keysym (keysym 255 225))
  251. (defconstant right-shift-keysym (keysym 255 226))
  252. (defconstant left-control-keysym (keysym 255 227))
  253. (defconstant right-control-keysym (keysym 255 228))
  254. (defconstant caps-lock-keysym (keysym 255 229))
  255. (defconstant shift-lock-keysym (keysym 255 230))
  256. (defconstant left-meta-keysym (keysym 255 231))
  257. (defconstant right-meta-keysym (keysym 255 232))
  258. (defconstant left-alt-keysym (keysym 255 233))
  259. (defconstant right-alt-keysym (keysym 255 234))
  260. (defconstant left-super-keysym (keysym 255 235))
  261. (defconstant right-super-keysym (keysym 255 236))
  262. (defconstant left-hyper-keysym (keysym 255 237))
  263. (defconstant right-hyper-keysym (keysym 255 238))
  264. ) ;; end eval-when
  265.  
  266.  
  267. ;;-----------------------------------------------------------------------------
  268. ;; Keysym mapping functions
  269.  
  270. (defun display-keyboard-mapping (display)
  271.   (declare (type display display))
  272.   (declare (values (simple-array keysym (display-max-keycode keysyms-per-keycode))))
  273.   (or (display-keysym-mapping display)
  274.       (setf (display-keysym-mapping display) (keyboard-mapping display))))
  275.  
  276. (defun keycode->keysym (display keycode keysym-index)
  277.   (declare (type display display)
  278.        (type card8 keycode)
  279.        (type card8 keysym-index)
  280.        (values keysym))
  281.   (let* ((mapping (display-keyboard-mapping display))
  282.      (keysym (aref mapping keycode keysym-index)))
  283.     (declare (type (simple-array keysym (* *)) mapping)
  284.          (type keysym keysym))
  285.     ;; The keysym-mapping is brain dammaged.
  286.     ;; Mappings for both-case alphabetic characters have the
  287.     ;; entry for keysym-index zero set to the uppercase keysym
  288.     ;; (this is normally where the lowercase keysym goes), and the
  289.     ;; entry for keysym-index one is zero.
  290.     (cond ((zerop keysym-index)            ; Lowercase alphabetic keysyms
  291.        (keysym-downcase keysym))
  292.       ((and (zerop keysym) (plusp keysym-index)) ; Get the uppercase keysym
  293.        (aref mapping keycode 0))
  294.       (t keysym))))
  295.  
  296. (defun keysym->character (display keysym &optional (state 0))
  297.   ;; Find the character associated with a keysym.
  298.   ;; STATE can be used to set character attributes.
  299.   ;; Implementation dependent function.
  300.   (declare (type display display)
  301.        (type keysym keysym)
  302.        (type card16 state))
  303.   (declare (values (or null character)))
  304.   (let* ((display-mappings (cdr (assoc keysym (display-keysym-translation display))))
  305.      (mapping (or ;; Find the matching display mapping
  306.               (dolist (mapping display-mappings)
  307.             (when (mapping-matches-p display state mapping)
  308.               (return mapping)))
  309.               ;; Find the matching static mapping
  310.               (dolist (mapping (gethash keysym *keysym->character-map*))
  311.             (when (mapping-matches-p display state mapping)
  312.               (return mapping))))))
  313.     (when mapping
  314.       (funcall (or (keysym-mapping-translate mapping) 'default-keysym-translate)
  315.            display state (keysym-mapping-object mapping)))))
  316.  
  317. (defun mapping-matches-p (display state mapping)
  318.   ;; Returns T when the modifiers and mask in MAPPING satisfies STATE for DISPLAY
  319.   (declare (type display display)
  320.        (type mask16 state)
  321.        (type list mapping))
  322.   (declare (values boolean))
  323.   (flet
  324.     ((modifiers->mask (display-mapping modifiers errorp &aux (mask 0))
  325.        ;; Convert MODIFIERS, which is a modifier mask, or a list of state-mask-keys into a mask.
  326.        ;; If ERRORP is non-nil, return NIL when an unknown modifier is specified,
  327.        ;; otherwise ignore unknown modifiers.
  328.        (declare (type list display-mapping)    ; Alist of (keysym . mask)
  329.         (type (or mask16 list) modifiers)
  330.         (type mask16 mask))
  331.        (declare (values (or null mask16)))
  332.        (if (numberp modifiers)
  333.        modifiers
  334.      (dolist (modifier modifiers mask)
  335.        (declare (type symbol modifier))
  336.        (let ((bit (position modifier (the simple-vector *state-mask-vector*) :test #'eq)))
  337.          (setq mask
  338.            (logior mask
  339.                (if bit
  340.                    (ash 1 bit)
  341.                  (or (cdr (assoc modifier display-mapping))
  342.                  ;; bad modifier
  343.                  (if errorp
  344.                      (return-from modifiers->mask nil)
  345.                    0))))))))))
  346.  
  347.     (let* ((display-mapping (get-display-modifier-mapping display))
  348.        (mapping-modifiers (keysym-mapping-modifiers mapping))
  349.        (modifiers (or (modifiers->mask display-mapping (or mapping-modifiers 0) t)
  350.               (return-from mapping-matches-p nil)))
  351.        (mapping-mask (or (keysym-mapping-mask mapping)    ; If no mask, use the default.
  352.                  (if mapping-modifiers            ; If no modifiers, match anything.
  353.                  *default-keysym-translate-mask*
  354.                    0)))
  355.        (mask (if (eq mapping-mask :modifiers)
  356.              modifiers
  357.            (modifiers->mask display-mapping mapping-mask nil))))
  358.       (declare (type mask16 modifiers mask))
  359.       (= (logand state mask) modifiers))))
  360.  
  361. (defun default-keysym-index (display keycode state)
  362.   ;; Returns a keysym-index for use with keycode->character
  363.   (declare (values card8))
  364.   (macrolet ((keystate-p (state keyword)
  365.            `(the boolean
  366.              (logbitp ,(position keyword *state-mask-vector*)
  367.                   ,state))))
  368.     (let* ((mapping (display-keyboard-mapping display))
  369.        (keysyms-per-keycode (array-dimension mapping 1))
  370.        (symbolp (and (> keysyms-per-keycode 2)
  371.              (state-keysymp display state character-set-switch-keysym)))
  372.        (result (if symbolp 2 0)))
  373.       (declare (type (simple-array keysym (* *)) mapping)
  374.            (type boolean symbolp)
  375.            (type card8 keysyms-per-keycode result))
  376.       (when (and (< result keysyms-per-keycode)
  377.          (keysym-shift-p display state (keysym-uppercase-alphabetic-p
  378.                          (aref mapping keycode 0))))
  379.     (incf result))
  380.       result)))
  381.  
  382. (defun keysym-shift-p (display state uppercase-alphabetic-p &key
  383.                shift-lock-xors
  384.                (control-modifiers
  385.              '#.(list left-meta-keysym left-super-keysym left-hyper-keysym)))
  386.   (declare (type display display)
  387.        (type card16 state)
  388.        (type boolean uppercase-alphabetic-p)
  389.        (type boolean shift-lock-xors));;; If T, both SHIFT-LOCK and SHIFT is the same
  390.                                       ;;; as neither if the character is alphabetic.
  391.   (declare (values boolean))
  392.   (macrolet ((keystate-p (state keyword)
  393.            `(the boolean
  394.              (logbitp ,(position keyword *state-mask-vector*)
  395.                   ,state))))
  396.     (let* ((controlp (or (keystate-p state :control)
  397.              (dolist (modifier control-modifiers)
  398.                (when (state-keysymp display state modifier)
  399.                  (return t)))))
  400.        (shiftp (keystate-p state :shift))
  401.        (lockp  (keystate-p state :lock))
  402.        (alphap (or uppercase-alphabetic-p
  403.                (not (state-keysymp display #.(make-state-mask :lock)
  404.                        caps-lock-keysym)))))
  405.       (declare (type boolean controlp shiftp lockp alphap))
  406.       ;; Control keys aren't affected by lock
  407.       (unless controlp
  408.     ;; Not a control character - check state of lock modifier
  409.     (when (and lockp
  410.            alphap
  411.            (or (not shiftp) shift-lock-xors))    ; Lock doesn't unshift unless shift-lock-xors
  412.       (setq shiftp (not shiftp))))
  413.       shiftp)))
  414.  
  415. ;;; default-keysym-index implements the following tables:
  416. ;;;
  417. ;;; control shift caps-lock character               character
  418. ;;;   0       0       0       #\a                      #\8
  419. ;;;   0       0       1       #\A                      #\8
  420. ;;;   0       1       0       #\A                      #\*
  421. ;;;   0       1       1       #\A                      #\*
  422. ;;;   1       0       0       #\control-A              #\control-8
  423. ;;;   1       0       1       #\control-A              #\control-8
  424. ;;;   1       1       0       #\control-shift-a        #\control-*
  425. ;;;   1       1       1       #\control-shift-a        #\control-*
  426. ;;;
  427. ;;; control shift shift-lock character               character
  428. ;;;   0       0       0       #\a                      #\8
  429. ;;;   0       0       1       #\A                      #\*
  430. ;;;   0       1       0       #\A                      #\*
  431. ;;;   0       1       1       #\A                      #\8
  432. ;;;   1       0       0       #\control-A              #\control-8
  433. ;;;   1       0       1       #\control-A              #\control-*
  434. ;;;   1       1       0       #\control-shift-a        #\control-*
  435. ;;;   1       1       1       #\control-shift-a        #\control-8
  436.  
  437. (defun keycode->character (display keycode state &key keysym-index
  438.                        (keysym-index-function #'default-keysym-index))
  439.   ;; keysym-index defaults to the result of keysym-index-function which
  440.   ;; is called with the following parameters:
  441.   ;; (char0 state caps-lock-p keysyms-per-keycode)
  442.   ;; where char0 is the "character" object associated with keysym-index 0 and
  443.   ;; caps-lock-p is non-nil when the keysym associated with the lock
  444.   ;; modifier is for caps-lock.
  445.   ;; STATE can also used for setting character attributes.
  446.   ;; Implementation dependent function.
  447.   (declare (type display display)
  448.        (type card8 keycode)
  449.        (type card16 state)
  450.        (type (or null card8) keysym-index)
  451.        (type (or null (function (base-char card16 boolean card8) card8))
  452.          keysym-index-function))
  453.   (declare (values (or null character)))
  454.   (let* ((index (or keysym-index
  455.             (funcall keysym-index-function display keycode state)))
  456.      (keysym (if index (keycode->keysym display keycode index) 0)))
  457.     (declare (type (or null card8) index)
  458.          (type keysym keysym))
  459.     (when (plusp keysym)
  460.       (keysym->character display keysym state))))
  461.  
  462. (defun get-display-modifier-mapping (display)
  463.   (labels ((keysym-replace (display modifiers mask &aux result)
  464.          (dolist (modifier modifiers result)
  465.            (push (cons (keycode->keysym display modifier 0) mask) result))))
  466.     (or (display-modifier-mapping display)
  467.     (multiple-value-bind (shift lock control mod1 mod2 mod3 mod4 mod5)
  468.         (modifier-mapping display)
  469.       (setf (display-modifier-mapping display)
  470.         (nconc (keysym-replace display shift #.(make-state-mask :shift))
  471.                (keysym-replace display lock #.(make-state-mask :lock))
  472.                (keysym-replace display control #.(make-state-mask :control))
  473.                (keysym-replace display mod1 #.(make-state-mask :mod-1))
  474.                (keysym-replace display mod2 #.(make-state-mask :mod-2))
  475.                (keysym-replace display mod3 #.(make-state-mask :mod-3))
  476.                (keysym-replace display mod4 #.(make-state-mask :mod-4))
  477.                (keysym-replace display mod5 #.(make-state-mask :mod-5))))))))
  478.  
  479. (defun state-keysymp (display state keysym)
  480.   ;; Returns T when a modifier key associated with KEYSYM is on in STATE
  481.   (declare (type display display)
  482.        (type card16 state)
  483.        (type keysym keysym))
  484.   (declare (values boolean))
  485.   (let* ((mapping (get-display-modifier-mapping display))
  486.      (mask (assoc keysym mapping)))
  487.     (and mask (plusp (logand state (cdr mask))))))
  488.  
  489. (defun mapping-notify (display request start count)
  490.   ;; Called on a mapping-notify event to update
  491.   ;; the keyboard-mapping cache in DISPLAY
  492.   (declare (type display display)
  493.        (type (member :modifier :keyboard :pointer) request)
  494.        (type card8 start count)
  495.        (ignore count start))
  496.   ;; Invalidate the keyboard mapping to force the next key translation to get it
  497.   (case request
  498.     (:modifier 
  499.      (setf (display-modifier-mapping display) nil))
  500.     (:keyboard
  501.      (setf (display-keysym-mapping display) nil))))
  502.  
  503. (defun keysym-in-map-p (display keysym keymap)
  504.   ;; Returns T if keysym is found in keymap
  505.   (declare (type display display)
  506.        (type keysym keysym)
  507.        (type (bit-vector 256) keymap))
  508.   (declare (values boolean))
  509.   ;; The keysym may appear in the keymap more than once,
  510.   ;; So we have to search the entire keysym map.
  511.   (do* ((min (display-min-keycode display))
  512.     (max (display-max-keycode display))
  513.     (map (display-keyboard-mapping display))
  514.     (jmax (min 2 (array-dimension map 1)))
  515.     (i min (1+ i)))
  516.       ((> i max))
  517.     (declare (type card8 min max jmax)
  518.          (type (simple-array keysym (* *)) map))
  519.     (when (and (plusp (aref keymap i))
  520.            (dotimes (j jmax)
  521.          (when (= keysym (aref map i j)) (return t))))
  522.       (return t))))
  523.  
  524. (defun character-in-map-p (display character keymap)
  525.   ;; Implementation dependent function.
  526.   ;; Returns T if character is found in keymap
  527.   (declare (type display display)
  528.        (type character character)
  529.        (type (bit-vector 256) keymap))
  530.   (declare (values boolean))
  531.   ;; Check all one bits in keymap
  532.   (do* ((min (display-min-keycode display))
  533.     (max (display-max-keycode display))
  534.     (jmax (array-dimension (display-keyboard-mapping display) 1))
  535.     (i min (1+ i)))
  536.       ((> i max))
  537.     (declare (type card8 min max jmax))
  538.     (when (and (plusp (aref keymap i))
  539.            ;; Match when character is in mapping for this keycode
  540.            (dotimes (j jmax)
  541.          (when (eql character (keycode->character display i 0 :keysym-index j))
  542.            (return t))))
  543.       (return t))))
  544.  
  545. (defun keysym->keycodes (display keysym)
  546.   ;; Return keycodes for keysym, as multiple values
  547.   (declare (type display display)
  548.        (type keysym keysym))
  549.   (declare (values (or null keycode) (or null keycode) (or null keycode)))
  550.   ;; The keysym may appear in the keymap more than once,
  551.   ;; So we have to search the entire keysym map.
  552.   (do* ((min (display-min-keycode display))
  553.     (max (display-max-keycode display))
  554.     (map (display-keyboard-mapping display))
  555.     (jmax (min 2 (array-dimension map 1)))
  556.     (i min (1+ i))
  557.     (result nil))
  558.       ((> i max) (values-list result))
  559.     (declare (type card8 min max jmax)
  560.          (type (simple-array keysym (* *)) map))
  561.     (dotimes (j jmax)
  562.       (when (= keysym (aref map i j))
  563.     (push i result)))))
  564.